package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.yiren.backstage.currency.entity.JudgeEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.entity.ProjectJudgeEntity;

import com.yiren.backstage.currency.entity.ProjectJudgeEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "project_judge".
*/
public class ProjectJudgeEntityDao extends AbstractDao<ProjectJudgeEntity, String> {

    public static final String TABLENAME = "project_judge";

    /**
     * Properties of entity ProjectJudgeEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property ParentId = new Property(1, String.class, "parentId", false, "PARENT_ID");
        public final static Property ProjectId = new Property(2, String.class, "projectId", false, "PROJECT_ID");
        public final static Property JudgeId = new Property(3, String.class, "judgeId", false, "JUDGE_ID");
    }

    private DaoSession daoSession;


    public ProjectJudgeEntityDao(DaoConfig config) {
        super(config);
    }
    
    public ProjectJudgeEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ProjectJudgeEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String parentId = entity.getParentId();
        if (parentId != null) {
            stmt.bindString(2, parentId);
        }
 
        String projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindString(3, projectId);
        }
 
        String judgeId = entity.getJudgeId();
        if (judgeId != null) {
            stmt.bindString(4, judgeId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ProjectJudgeEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String parentId = entity.getParentId();
        if (parentId != null) {
            stmt.bindString(2, parentId);
        }
 
        String projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindString(3, projectId);
        }
 
        String judgeId = entity.getJudgeId();
        if (judgeId != null) {
            stmt.bindString(4, judgeId);
        }
    }

    @Override
    protected final void attachEntity(ProjectJudgeEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public ProjectJudgeEntity readEntity(Cursor cursor, int offset) {
        ProjectJudgeEntity entity = new ProjectJudgeEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // parentId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // projectId
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // judgeId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ProjectJudgeEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setParentId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setProjectId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setJudgeId(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
     }
    
    @Override
    protected final String updateKeyAfterInsert(ProjectJudgeEntity entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(ProjectJudgeEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ProjectJudgeEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getProjectJudgeEntityDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getProjectEntityDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getJudgeEntityDao().getAllColumns());
            builder.append(" FROM project_judge T");
            builder.append(" LEFT JOIN project_judge T0 ON T.\"PARENT_ID\"=T0.\"ID\"");
            builder.append(" LEFT JOIN project T1 ON T.\"PROJECT_ID\"=T1.\"ID\"");
            builder.append(" LEFT JOIN judge T2 ON T.\"JUDGE_ID\"=T2.\"ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ProjectJudgeEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        ProjectJudgeEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ProjectJudgeEntity parentProjectJudgeEntity = loadCurrentOther(daoSession.getProjectJudgeEntityDao(), cursor, offset);
        entity.setParentProjectJudgeEntity(parentProjectJudgeEntity);
        offset += daoSession.getProjectJudgeEntityDao().getAllColumns().length;

        ProjectEntity project = loadCurrentOther(daoSession.getProjectEntityDao(), cursor, offset);
        entity.setProject(project);
        offset += daoSession.getProjectEntityDao().getAllColumns().length;

        JudgeEntity judgeEntity = loadCurrentOther(daoSession.getJudgeEntityDao(), cursor, offset);
        entity.setJudgeEntity(judgeEntity);

        return entity;    
    }

    public ProjectJudgeEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ProjectJudgeEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ProjectJudgeEntity> list = new ArrayList<ProjectJudgeEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ProjectJudgeEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ProjectJudgeEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
